package listeners;

import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.locale.converters.IntegerLocaleConverter;

import util.Conversions;

import auser.AccantonamentoBar;
import auser.CausaleContabile;
import auser.Circolo;
import auser.Comune;
import auser.Counters;
import auser.MovimentoBar;
import auser.PianoDeiConti;
import auser.ScritturaDiPrimaNota;
import auser.Socio;
import auser.Tessera;
import auser.Utente;
import auser.VoceDiPrimaNota;
import auser.VoceDiPrimaNotaDaScrittura;

import com.db4o.Db4o;
import com.db4o.config.annotations.Indexed;
import com.db4o.ext.ExtDb4o;
import com.db4o.ext.ObjectCallbacks;

import converters.DateLocaleConverterCustom;
import converters.DoubleLocalConverterCustom;
import converters.IntegerLocalConverterCustom;

public class ApplicationListen implements  ServletContextListener{

	public void contextInitialized(ServletContextEvent event) {
	    ServletContext servCtx = event.getServletContext();
	    servCtx.log("Application Auser started");
	    configureBeans();
	    configureDb4o();
	    try {     
	        Context initContext = new InitialContext();
	    } catch (Exception e) { e.printStackTrace(); }
	
	}
	
	private void configureBeans(){
		ConvertUtils.register(new DateLocaleConverterCustom( Locale.ITALY,"dd/MM/yyyy"),Date.class );
		ConvertUtils.register(new DoubleLocalConverterCustom( Locale.ITALY),Double.class );
		ConvertUtils.register(new IntegerLocalConverterCustom( Locale.ITALY),Integer.class );
		
	}
	private void configureDb4o(){
		Db4o.configure().objectClass(Socio.class).cascadeOnUpdate(true);
	  	Db4o.configure().freespace().discardSmallerThan(Integer.MAX_VALUE);
		Db4o.configure().callConstructors(true);
		Db4o.configure().lockDatabaseFile(false);
		Db4o.configure().testConstructors(false);
		Db4o.configure().blockSize(8);
		Db4o.configure().activationDepth(5);
		Db4o.configure().updateDepth(5);
		Db4o.configure().weakReferences(false);
		Db4o.configure().weakReferenceCollectionInterval(999999999);
		Db4o.configure().messageLevel(1); 
		boolean index = false;  
		
		// AccantonamentoBar
		ExtDb4o.configure().objectClass(AccantonamentoBar.class).objectField("dataAccantonamento").indexed(index);
		ExtDb4o.configure().objectClass(AccantonamentoBar.class).objectField("annoCompetenza").indexed(index);
		ExtDb4o.configure().objectClass(AccantonamentoBar.class).objectField("circolo").indexed(index);
		ExtDb4o.configure().objectClass(AccantonamentoBar.class).objectField("progressivoOperazione").indexed(index);
		
		//CausaleContabile
		ExtDb4o.configure().objectClass(CausaleContabile.class).objectField("descrizione").indexed(index);
		ExtDb4o.configure().objectClass(CausaleContabile.class).objectField("codice");
		
		//Circolo
		ExtDb4o.configure().objectClass(Circolo.class).objectField("codice").indexed(index);
		ExtDb4o.configure().objectClass(Circolo.class).objectField("descrizione").indexed(index);
		ExtDb4o.configure().objectClass(Circolo.class).objectField("indirizzo").indexed(index);
		ExtDb4o.configure().objectClass(Circolo.class).objectField("gruppo").indexed(index);
		
		//comune
		ExtDb4o.configure().objectClass(Comune.class).objectField("descrizione").indexed(index);
		
		//socio
		ExtDb4o.configure().objectClass(Socio.class).objectField("cognome").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("nome").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("comune").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("annoIscrizione").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("dataNascita").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("meseNascita").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("annoNascita").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("luogoNascita").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("circolo").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("sesso").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("numeroRegistro").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("assistenzaPersoneDifficolta").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("trasportoAmmalati").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("accompagnatore").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("personaDiCompagnia").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("consegnaPasti").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("praticheAmministrative").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("animazione").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("intrattenimento").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("puliziaAbitazione").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("campiGioco").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("barCircolo").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("organizzaViaggi").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("turismo").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("conferenze").indexed(index); 
		ExtDb4o.configure().objectClass(Socio.class).objectField("laboratori").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("formazione").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("attivitaMotorie").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("custodiaEdifici").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("vigilanza").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("manutenzioni").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("legna").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("ufficio").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("computer").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("lunedi").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("martedi").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("mercoledi").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("giovedi").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("venerdi").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("sabato").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("domenica").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("mattina").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("pomeriggio").indexed(index);
		ExtDb4o.configure().objectClass(Socio.class).objectField("altreAttivita").indexed(index);
		//counters
		ExtDb4o.configure().objectClass(Counters.class).objectField("anno").indexed(index);  
		ExtDb4o.configure().objectClass(Counters.class).objectField("className").indexed(index);  
		ExtDb4o.configure().objectClass(Counters.class).objectField("circolo").indexed(index);  
		
		//Utente
		ExtDb4o.configure().objectClass(Utente.class).objectField("userId").indexed(index);  
		ExtDb4o.configure().objectClass(Utente.class).objectField("descrizione").indexed(index);  
		ExtDb4o.configure().objectClass(Utente.class).objectField("circolo").indexed(index);  
		
		//MovimentoBar
		ExtDb4o.configure().objectClass(MovimentoBar.class).objectField("dataMovimento").indexed(index); 
		ExtDb4o.configure().objectClass(MovimentoBar.class).objectField("annoCompetenza").indexed(index); 
		ExtDb4o.configure().objectClass(MovimentoBar.class).objectField("descrizione").indexed(index); 
		ExtDb4o.configure().objectClass(MovimentoBar.class).objectField("circolo").indexed(index); 
		ExtDb4o.configure().objectClass(MovimentoBar.class).objectField("progressivoOperazione").indexed(index); 
		
		//PianoDeiConti
		ExtDb4o.configure().objectClass(PianoDeiConti.class).objectField("circolo").indexed(index);
		ExtDb4o.configure().objectClass(PianoDeiConti.class).objectField("descrizione").indexed(index);
		ExtDb4o.configure().objectClass(PianoDeiConti.class).objectField("codice").indexed(index);
		ExtDb4o.configure().objectClass(PianoDeiConti.class).objectField("l1").indexed(index);
		ExtDb4o.configure().objectClass(PianoDeiConti.class).objectField("l2").indexed(index);
		ExtDb4o.configure().objectClass(PianoDeiConti.class).objectField("l3").indexed(index);
		ExtDb4o.configure().objectClass(PianoDeiConti.class).objectField("l4").indexed(index);
		ExtDb4o.configure().objectClass(PianoDeiConti.class).objectField("livello").indexed(index);
		ExtDb4o.configure().objectClass(PianoDeiConti.class).objectField("tipo").indexed(index);
		ExtDb4o.configure().objectClass(PianoDeiConti.class).objectField("voceCorrelata").indexed(index);
		
		
		//ScritturaPrimaNota
		ExtDb4o.configure().objectClass(ScritturaDiPrimaNota.class).objectField("annoCompetenza").indexed(index);
		ExtDb4o.configure().objectClass(ScritturaDiPrimaNota.class).objectField("dataScrittura").indexed(index);
		ExtDb4o.configure().objectClass(ScritturaDiPrimaNota.class).objectField("circolo").indexed(index);
		ExtDb4o.configure().objectClass(ScritturaDiPrimaNota.class).objectField("descrizione").indexed(index);
		ExtDb4o.configure().objectClass(ScritturaDiPrimaNota.class).objectField("progressivoOperazione").indexed(index);
		
		//Tessera
		ExtDb4o.configure().objectClass(Tessera.class).objectField("annoCompetenza").indexed(index);
		ExtDb4o.configure().objectClass(Tessera.class).objectField("numeroTessera").indexed(index);
		ExtDb4o.configure().objectClass(Tessera.class).objectField("socio").indexed(index);
		
		//VoceDiPrimaNota
		ExtDb4o.configure().objectClass(VoceDiPrimaNota.class).objectField("parent").indexed(index);
		ExtDb4o.configure().objectClass(VoceDiPrimaNota.class).objectField("circolo").indexed(index);
		ExtDb4o.configure().objectClass(VoceDiPrimaNota.class).objectField("vocePianoDeiConti").indexed(index);
		ExtDb4o.configure().objectClass(VoceDiPrimaNota.class).objectField("descrizione").indexed(index);
		ExtDb4o.configure().objectClass(VoceDiPrimaNota.class).objectField("private Date dataScrittura").indexed(index);
		ExtDb4o.configure().objectClass(VoceDiPrimaNota.class).objectField("codice").indexed(index);
		
		//VoceDiPrimaNotaDaScrittura
		ExtDb4o.configure().objectClass(VoceDiPrimaNotaDaScrittura.class).objectField("riga").indexed(index);
		ExtDb4o.configure().objectClass(VoceDiPrimaNotaDaScrittura.class).objectField("quadro").indexed(index);
	}
  
	 
	public void contextDestroyed(ServletContextEvent event){
		ServletContext context = event.getServletContext(); 
		context.log("Application Auser Stopped");
		
	}
}    